from django.conf import settings
import RDF

__all__ = ["Schema"]

class Schema(object):
	"""
	This is a decorator class for Django objects that enables
	them to be populated from an RDF model. It is used like
	so,

	>>> from django.db import models
	>>> from duetopia import rdf 
	>>> from RDF import Uri
	>>> dctitle = Uri("http://purl.org/dc/elements/1.1/title")
	>>> class MyModel(models.Model):
	...     class RDF(rdf.Schema):
	...         properties = (
	...             ("title", rdf.parser.String(dctitle)),
	...         )
	...     ### this property is obligatory and uniquely
	...     ### identifies an instance.
	...     uri = models.URLField(unique=True)
	...
        >>>
	
	The instance of the RDF schema is made available on the
	model as a .rdf property:
	
	>>> hasattr(MyModel._meta, "rdf")
	True
	"""
	properties = []
	def __init__(self, model):
		self.django_model = model
		self.storage = RDF.Storage(
			storage_name = settings.DUETOPIA_RDF_STORAGE,
			name = self.name,
			options_string = settings.DUETOPIA_RDF_OPTIONS
			)

	@property
	def model(self):
		return RDF.Model(self.storage)
	
	@classmethod
	def contribute_to_class(cls, django_model, name):
		setattr(django_model._meta, "rdf", cls(django_model))
		
if __name__ == '__main__':
	import doctest
	doctest.testmod()
